﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Reflection;
using System.Diagnostics;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.UserCode;
using System.Data;

namespace CKS.Sandbox.Admin.Layouts.CKS.Sandbox.Admin
{
    public partial class ManageSandboxSolutions : LayoutsPageBase
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            LoadGridView();
        }

        private void LoadGridView()
        {
            #region Create Data Table
            DataTable dt = new DataTable();
            dt.Columns.Add("SiteCollectionHeader");
            dt.Columns.Add("SiteCollectionUrl");
            dt.Columns.Add("SolutionName");
            dt.Columns.Add("SolutionID");
            dt.Columns.Add("SolutionTitle");
            dt.Columns.Add("SolutionDescription");
            dt.Columns.Add("SolutionStatus");
            dt.Columns.Add("SolutionResourceQuota");
            #endregion

            foreach (SPWebApplication webApp in SPFarm.Local.Services.GetValue<SPWebService>("").WebApplications)
            {
                #region Loop through each Web Application, and retrieve all Sandbox Solutions
                foreach (SPSite site in webApp.Sites)
                {
                    SPList list = site.GetCatalog(SPListTemplateType.SolutionCatalog);
                    if (list == null) { continue;  }

                    foreach (SPListItem item in list.Items)
                    {
                        DataRow row = dt.NewRow();

                        #region Set row values
                        row["SiteCollectionHeader"] = site.Url + " (" + site.RootWeb.Title + ") (Quota " + site.Quota.UserCodeMaximumLevel +")";
                        row["SiteCollectionUrl"] = site.Url;
                        row["SolutionName"] = item.Name;
                        row["SolutionID"] = item["SolutionId"];

                        if (item["SolutionTitle"] != null)
                        {
                            row["SolutionTitle"] = item["SolutionTitle"];
                        }
                        else
                        {
                            row["SolutionTitle"] = "";
                        }

                        if (item["SolutionDescription"] != null)
                        {
                            row["SolutionDescription"] = item["SolutionDescription"];
                        }
                        else
                        {
                            row["SolutionDescription"] = "";
                        }
                        
                        if (item["Status"] != null)
                        {
                            if (item["Status"].ToString().StartsWith("1"))
                            {
                                row["SolutionStatus"] = "Activated";
                            }
                            else if (item["Status"].ToString().StartsWith("2"))
                            {
                                row["SolutionStatus"] = "Disabled";
                            }
                        }
                        else
                        {
                            row["SolutionStatus"] = "Deactivated";
                        }

                        if (item["ResourceQuota"] != null)
                        {
                            if (item["ResourceQuota"].ToString().Length > 4)
                            {
                                // take the first 4 digits of the Resource Quota
                                row["SolutionResourceQuota"] = item["ResourceQuota"].ToString().Substring(0, 4);
                            }
                            else if (item["ResourceQuota"].ToString().StartsWith("0;#"))
                            {
                                row["SolutionResourceQuota"] = "0";
                            }
                            else
                            {
                                row["SolutionResourceQuota"] = item["ResourceQuota"];
                            }
                        }
                        else
                        {
                            row["SolutionResourceQuota"] = "";
                        }
                        #endregion

                        dt.Rows.Add(row);
                    }
                }
                #endregion
            }

            dt.AcceptChanges();
            DataView dv = dt.DefaultView;
            dv.Sort = "SiteCollectionUrl ASC";


            grdSolutions.DataSource = dv;
            grdSolutions.DataBind();
        }

    }
}
